﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.GuaZhiDuanLianService;

namespace CommunitySystem
{
    public partial class AddGuaZhiEmpWindow : ChildWindow
    {
        public string OrgIDs = "";
        private JinShengService.JinShengServiceClient _Client = new JinShengService.JinShengServiceClient();
        private GuaZhiDuanLianServiceClient Client1 =
            new GuaZhiDuanLianServiceClient();
        public ObservableCollection<JinShengService.Emp_BasicInfoModel> SelectItems = new ObservableCollection<JinShengService.Emp_BasicInfoModel>();
        private int ExameID;
        public AddGuaZhiEmpWindow(string orgids,int exameid)
        {
            InitializeComponent();
            OrgIDs = orgids;
            ExameID = exameid;
            Client1.InsertGuaZhiEmpCompleted += new EventHandler<InsertGuaZhiEmpCompletedEventArgs>(Client1_InsertGuaZhiEmpCompleted);
            _Client.GetEmpBasicInfoAccordingToDutyCompleted +=
                new EventHandler<JinShengService.GetEmpBasicInfoAccordingToDutyCompletedEventArgs>(_Client_GetEmpBasicInfoAccordingToDutyCompleted);
            tbCondition.AddHandler(TextBox.MouseLeftButtonDownEvent, new MouseButtonEventHandler(TextBox_MouseLeftButtonDown), true);
            Client1.GetGuaZhiDuanLianEmpsCompleted += new EventHandler<GetGuaZhiDuanLianEmpsCompletedEventArgs>(Client1_GetGuaZhiDuanLianEmpsCompleted);
        }

        void Client1_InsertGuaZhiEmpCompleted(object sender, InsertGuaZhiEmpCompletedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void ChildWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnAdminSearch_Click(null, null);
            }
        }

        private void TextBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!string.IsNullOrEmpty(tbCondition.Text) && tbCondition.Text.Equals("请输入姓名或证件号码"))
            {
                tbCondition.SelectAll();
            }
        }

        private void tbCondition_KeyDown(object sender, KeyEventArgs e)
        {
            if (!string.IsNullOrEmpty(tbCondition.Text) && tbCondition.Text.Equals("请输入姓名或证件号码"))
            {
                tbCondition.FontStyle = FontStyles.Normal;
                //tbCondition.Text = e.Key.ToString();
                tbCondition.Foreground = new SolidColorBrush(Colors.Black);
            }
        }

        public string empids = "";
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            SelectItems = null;
            var source = lb.ItemsSource as System.Collections.ObjectModel.ObservableCollection<CommunitySystem.JinShengService.Emp_BasicInfoModel>;
            SelectItems = new ObservableCollection<JinShengService.Emp_BasicInfoModel>(source.Where(c => c.IsSelected == true));
            if (SelectItems.Count > 0)
            {
                if (MessageBox.Show("确定添加挂职锻炼人员吗？", "系统提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    foreach(JinShengService.Emp_BasicInfoModel i in SelectItems)
                    {
                        empids += i.ID + ",";
                    }
                    if (!string.IsNullOrEmpty(empids))
                        Client1.GetGuaZhiDuanLianEmpsAsync(ExameID, empids.Substring(0, empids.Length - 1));
                    
                }
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要添加的挂职锻炼人员！");

        }

        void Client1_GetGuaZhiDuanLianEmpsCompleted(object sender, GetGuaZhiDuanLianEmpsCompletedEventArgs e)
        {
            if (e.Result == true)
                Client1.InsertGuaZhiEmpAsync(empids.Substring(0, empids.Length - 1), ExameID);
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("所选人员已在挂职锻炼中，不可重复挂职锻炼！");
                empids = "";
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        private void btnAdminSearch_Click(object sender, RoutedEventArgs e)
        {
            string nameOrCard = "1=1";
            if (!string.IsNullOrEmpty(tbCondition.Text) && !tbCondition.Text.Equals("请输入姓名或证件号码"))
            {
                nameOrCard = tbCondition.Text;
                _Client.GetEmpBasicInfoAccordingToDutyAsync(string.Format(@" OrgID in ({0}) and KeChuJi = 0 and (Name like '%{1}%' or CardNo like '%{1}%') ", OrgIDs, nameOrCard));
                // _empClient.GetEmp_BasicInfoAsync(_OrgID + "", "0", "", "", 0, 20, "在职", string.Format("emp.Name like '%{0}%' or emp.CardNo like '%{0}%'", nameOrCard));
            }
            else
            {
                _Client.GetEmpBasicInfoAccordingToDutyAsync(string.Format(" OrgID in ({0}) and KeChuJi = 0 ", OrgIDs));
            }
        }

        void _Client_GetEmpBasicInfoAccordingToDutyCompleted(object sender, JinShengService.GetEmpBasicInfoAccordingToDutyCompletedEventArgs e)
        {
            lb.ItemsSource = null;
            lb.ItemsSource = e.Result;
        }
    }
}
